#coding=utf-8
from django.template.loader import render_to_string
from django.utils.encoding import force_unicode
from django.utils.html import conditional_escape
from django.utils.safestring import mark_safe
from BaiduMap_JS.utils import SelectPositionOptions

__author__ = 'czq'
from django import forms

class SelectPositionForm(forms.TextInput):
    def __init__(self,longitude_name='longitude',latitude_name='latitude',address_name='address',iframe_name='iframe_name',dialog_width='640px',dialog_height='600px', attrs=None,**kwargs):
        self.options=SelectPositionOptions(longitude_name=longitude_name,latitude_name=latitude_name,address_name=address_name,iframe_name=iframe_name,dialog_width=dialog_width,dialog_height=dialog_height)
        super(SelectPositionForm,self).__init__(attrs)

    def render(self, name, value, attrs=None):
        #return self.prefix
        #return str(attrs)
        uOptions=self.options.copy()

        if (name.find('set')==-1):
            index=''
            indexSet=''
        else:
            import re
            index=re.findall('set-(.*?)-',name)[0]
            indexSet=name[:name.rfind('-')+1]

        uOptions.update({
            "iframe_name":self.options["iframe_name"]+indexSet+index,
        })
        value=conditional_escape(force_unicode(value))
        if value==None or value=='None':
            value=""

        context={
            'options':uOptions,
            'name':name,
            'index':index,
            'indexSet':indexSet,
            "value":value
        }
        #html = super(SelectPositionForm,self).render(name,value)
        #html+=mark_safe(render_to_string('baidu_map_select.html',context))

        #html+='<img height="10" width="10" alt="选择地点" src="/static/admin/img/icon_addlink.gif">'

        #return name
        return mark_safe(render_to_string('BaiduMap_JS/baidu_map_select.html',context))
